package com.example.home.view

import androidx.fragment.app.activityViewModels
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.RoutePath
import com.example.common.mvi.BaseMviFragment
import com.example.home.R
import com.example.home.adapter.HomeViewPageAdapter
import com.example.home.databinding.FragmentHomeBinding
import com.example.home.entity.RecommendVideoEntity
import com.example.login.intent.SimpleViewIntent
import com.example.login.state.SimpleUiState
import com.example.login.vm.SimpleViewModel
import com.example.main.ui.home.entity.SimpleTypeEntity
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RoutePath.ROUTE_HOME)
class HomeFragment : BaseMviFragment<FragmentHomeBinding>() {
    private val mViewModel : SimpleViewModel by activityViewModels()
    private val fragments = mutableListOf<HomeListFragment>()
    private lateinit var homeVpAdapter : HomeViewPageAdapter
    private var tabList = mutableListOf<SimpleTypeEntity>()
    private val searchHotWords = listOf("耳朵里塞驴毛","王子大蛋","小学生","骷髅军团长","咩")

    override fun initViews() {
        binding.tvbSearch.setDatas(searchHotWords)

        binding.ivMsg.setOnClickListener {
            jumpToMsgPage()
        }
        binding.ivTypeMenu.setOnClickListener {
            showAllTypes()
        }
        binding.vp2Home.registerOnPageChangeCallback(object : OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
            }
        })
        homeVpAdapter = HomeViewPageAdapter(fragments,this@HomeFragment.childFragmentManager,lifecycle)
        binding.vp2Home.adapter = homeVpAdapter
        TabLayoutMediator(binding.tabTypeLayout,binding.vp2Home){tab,p->
            tab.text  = tabList[p].typename
        }.attach()
    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(SimpleViewIntent.TypeToServer)
    }

    override suspend fun collectUiState() {
        mViewModel.uiState.collect{
            when(it){
                is SimpleUiState.TypeSuccess->initTabFragments(it.data)
                is SimpleUiState.TypeFailure->showToastMessage(it.msg)
                else->{}
            }
        }
    }

    private fun initTabFragments(data: List<SimpleTypeEntity>) {
        if(data.isNotEmpty()){
            tabList.clear()
            fragments.clear()
            tabList.add(SimpleTypeEntity("",0,-1,0,"","推荐"))
            fragments.add(HomeListFragment.newInstance(-1,""))
        }
        data.forEach{
            tabList.add(it)
            fragments.add(HomeListFragment.newInstance(it.id,it.channelid))
        }
        tabList.forEach{
            binding.tabTypeLayout.addTab(binding.tabTypeLayout.newTab().setText(it.typename))
        }
        homeVpAdapter.notifyDataSetChanged()
    }


    private fun showAllTypes() {
        showToastMessage("视频类型")
    }

    private fun jumpToMsgPage() {
        showToastMessage("消息页面")
    }

    override fun onResume() {
        super.onResume()
        binding.tvbSearch.startViewAnimator()
    }

    override fun onStop() {
        super.onStop()
        binding.tvbSearch.stopViewAnimator()
    }
    override fun getLayoutId(): Int  = R.layout.fragment_home

}